/**
 * @函数功能:工单操作  工单列表表格操作列和详情界面右侧操作
 *
 */
import setMenu from "@/permission";
import {
  receiveOrRejectApi,
  changeSensitivityWorkUser,
  governSensitivityWork,
  governSelfExamApi,
} from "api/taskCenterApi";
export const orderOptMixin = {
  data() {
    return {
      loadingProp: { selfInspect: false, handler: false },

      isShowCloseOrder: false, //申诉
      isShowDeliver: false, //转交 是否展示
      delayDiaShow: false, //延期 是否展示
      //处理使用
      title: "",
      srcH5: "",
      apiDiaShow: false,
    };
  },
  methods: {
    isDisOptBtn(row) {
      return ["1", "3"].includes(row.appeal) ? true : false;
    },
    //处理按钮（自查的源）
    selfInspectionHandle(row) {
      this.title = "自查";
      this.$set(row, "loading", true);
      this.loadingProp.handler = true;
      let processIds = [row.processId];
      governSelfExamApi({ processIds, type: row.type, source: row.source }).then((res) => {
        console.log("[ res ] >", res);
        this.srcH5 = res.data;
        this.loadingProp.handler = false;
        this.$set(row, "loading", false);
        this.apiDiaShow = true;
      });
    },
    //处理
    handle(row) {
      let eventId = row ? "handler" : "batchhandler";
      let eventLable = row ? "处理" : "批量处理";
      this.btnOmegaTrack(eventId, eventLable);

      console.log(this.source);
      if (row?.appeal == "3") {
        this.title = "处理中";
        if (["api-api-0:HTTP"].includes(this.source)) {
          let url =
            "https://im.xiaojukeji.com/channel?uid=3555&token=7117629f6a263047fe9fd6b22224c9ba&id=2021193919427815680";
          this.$alert(
            `自提交日起，处理完成预计需要2个自然日，请等待。详情可加入“<a href="${url}" target="_blank">数据安全治理平台问题反馈群</a>”咨询`,
            "处理中",
            {
              showConfirmButton: false,
              dangerouslyUseHTMLString: true,
            },
          );
        } else {
          this.$alert(
            `正在处理中，请等待。详情可加入“<a href="${url}" target="_blank">数据安全治理平台问题反馈群</a>”咨询`,
            "处理中",
            {
              showConfirmButton: false,
              dangerouslyUseHTMLString: true,
            },
          );
        }
        return;
      }
      this.title = "处理";
      //这两个属性决定这一个表格哪个行的哪个按钮展示loading
      if (row) {
        this.$set(row, "loading", true);
        this.loadingProp.handler = true;
      }
      governSensitivityWork({ processId: row.processId }).then((res) => {
        if (row) {
          this.$set(row, "loading", false);
          this.loadingProp.handler = false;
        }
        this.srcH5 = res.data;
        this.apiDiaShow = true;
      });
    },
    //延期
    delayDateHandle(row) {
      //埋点
      let eventId = row ? "delay" : "batchdelay";
      let eventLable = row ? "延期" : "批量延期";
      this.btnOmegaTrack(eventId, eventLable);

      this.delayDiaShow = true;
      this.selRows = row ? [row] : this.selRows;
    },
    //认领和驳回
    receiveOrRejectHandle(row, isReceive = false) {
      //认领 埋点
      if (isReceive) {
        let eventId = row ? "claim" : "batchclaim";
        let eventLable = row ? "认领" : "批量认领";
        this.btnOmegaTrack(eventId, eventLable);
      }

      let rows = row ? [{ ...row, isReceive }] : this.selRows.map((item) => ({ ...item, isReceive }));
      let content = isReceive ? "工单认领，是否继续？（认领后仍可转交或申诉）" : "工单驳回，是否继续？";
      this.utils.$confirm({ content }, () => {
        receiveOrRejectApi(rows).then((res) => {
          console.log("[ res ] >", res);
          if (res.code === "0") {
            let message = isReceive ? "认领成功，请至”进行中“菜单进行治理" : "驳回成功";
            this.message.success({ message });
            //工单列表才会执行
            this.getOrderClass?.(); //工单分类
            this.reset?.();
            setMenu?.(); //初始化侧边栏数量
            // 详情界面执行 认领刷新界面 驳回跳转到列表页
            isReceive ? this.refreshPage?.() : this.gobackListPage?.();
          }
        });
      });
    },

    loaded() {
      let oldAction = "";
      window.addEventListener(
        "message",
        (data) => {
          let action = data.data?.res?.action;
          if (action && action != oldAction) {
            oldAction = action;
            this.apiDiaShow = false;
            let loading = this.openLoading();
            setTimeout(() => {
              loading.close();
              //列表界面使用
              this.initPage?.();
              //详情调用
              this.refreshPage?.();
            }, 5000);
          }
          console.log("[ data ] >", data);
        },
        false,
      );
    },
    // 转交
    handleDeliver(row) {
      let eventId = row ? "changeuser" : "batchchangeuser";
      let eventLable = row ? "转交" : "批量转交";
      this.btnOmegaTrack(eventId, eventLable);

      this.isShowDeliver = true;
      this.isBatch = row ? false : true;
      this.selRows = row ? [row] : this.selRows;
    },
    // 转交 确定成功回调
    deliverConfirmSuccess(ruleForm) {
      let processIds = this.selRows.map((item) => item.processId);
      let { account, remark, businessKeep, businessLineName } = ruleForm;
      this.$refs.accountChangeRef.openBtnLoading();
      changeSensitivityWorkUser({
        account,
        remark,
        businessKeep,
        businessLineName: businessKeep === "0" ? businessLineName : "",
        processIds,
      }).then((res) => {
        this.$refs.accountChangeRef.closeBtnLoading();
        let state = this.message.state(res);
        if (!state) return;
        this.isShowDeliver = false;
        //列表界面调用
        this.reset?.();
        //详情界面调用
        this.gobackListPage?.(); //回到列表界面
      });
    },
    // 申诉
    handleCloseOrder(row) {
      this.isShowCloseOrder = true;
      //申诉才埋点
      if (!row?.appeal) {
        let eventId = row ? "appeal" : "batchappeal";
        let eventLable = row ? "申诉" : "批量申诉";
        this.btnOmegaTrack(eventId, eventLable);
      }
      this.selRows = row ? [row] : this.selRows;
    },
  },
};
